// 配置路由

import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '@/views/Login'
import Layout from '@/views/Layout'
import Home from '@/views/Home'
// import User from '@/views/User'
// import UserEdit from '@/views/User/UserEdit'
// import Search from '@/views/Search'
// import SearchResult from '@/views/Search/SearchResult'
// import ArticleDetail from '@/views/ArticleDetail'
// import Chat from '@/views/Chat'
import store from '@/store'
Vue.use(VueRouter)

const routes = [

  {
    path: '/',
    redirect: '/layout'
  },
  {
    path: '/login',
    // component: Login,
    component: () => import('@/views/Login'), // 路由懒加载的语法
    beforeEnter: (to, from, next) => { // 路由局部守卫
      // 需求：登陆完成后，想再去/login，就让他留在原地
      if (store.state.token.length > 0) {
        next(false)
      } else {
        next() // 如果不写放行，路由一个都跳不了了
      }
    }
  },
  {
    path: '/layout',
    component: Layout,
    // component: () => import('@/views/Layout'),
    redirect: '/layout/home',
    children: [
      {
        path: 'home',
        component: Home,
        meta: { // 为了页面滚动效果配置路由参数，也可以用vuex
          isScroll: true, // 为了在路由守卫里进行判断
          top: 0 // 记录滚动条高度
        }
      },
      {
        path: 'user',
        // component: User
        component: () => import('@/views/User')
      }
    ]
  },
  {
    path: '/search',
    // component: Search
    component: () => import('@/views/Search')
  },
  {
    path: '/search/:kw',
    // component: SearchResult
    component: () => import('@/views/Search/SearchResult')
  },
  {
    path: '/article',
    // component: ArticleDetail
    component: () => import('@/views/ArticleDetail')
  },
  {
    path: '/edit',
    // component: UserEdit
    component: () => import('@/views/User/UserEdit')
  }, {
    path: '/chat',
    // component: Chat
    component: () => import('@/views/Chat')
  }

]

const router = new VueRouter({
  routes
})

// 全局前置守卫
// router.beforeEach((to, from, next) => {
// // 需求：登陆完成后，想再去/login，就让他留在原地
//   if (store.state.token.length > 0 && to.path === '/login') {
//     next(false)
//   } else {
//     next() // 如果不写放行，路由一个都跳不了了
//   }
// })

export default router
